package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.po.ReceiverLayer;
import com.colorlight.cloud.complete.entity.po.ReceiverTemperatureControlParam;
import com.colorlight.cloud.gateway.api.message.constraint.schedule.ScheduleActions;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 接收卡上报
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class RcvCardMonitorReport implements CompleteEntityEvent {

    /**
     * 接收卡信息
     */
    private List<ReceiverLayer> receiverInfo;

    /**
     * 温控继电器参数，获取失败时为null
     */
    private List<ReceiverTemperatureControlParam> tempRelayParam;

    @Override
    public RcvCardMonitorReport byteConvertObject(ByteString bytes) {
        CompleteEvent.RcvCardMonitorReport rcvCardMonitorReport = null;
        try {
            rcvCardMonitorReport = CompleteEvent.RcvCardMonitorReport.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(rcvCardMonitorReport);
        this.setReceiverInfo(rcvCardMonitorReport.getReceiverInfoList().stream()
                .map(p -> new ReceiverLayer().formProto(p)).collect(Collectors.toList()));
        this.setTempRelayParam(rcvCardMonitorReport.getTempRelayParamList().stream()
                .map(p -> new ReceiverTemperatureControlParam().formProto(p)).collect(Collectors.toList()));
        return this;
    }
}
